const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:['厦门市','深圳市'],
    index:0,
    active:"y",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var city=wx.getStorageInfoSync('userinfo').CityName;
    if(!city){
      city='厦门市';
    }

    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    time = time.split('-');
    var timearr = time[0] + '-' + time[1];
    that.setData({
      firstday: timearr + '-01',
      showfirst: time[0] + '年' + time[1]+'月',
      today: time[0] + '-' + time[1] + "-" + time[2],
      city: city,
      types: "营业"
    })
    that.getorder();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  bindDateChange: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var dates = e.detail.value.split('-');
    that.setData({
      showfirst: dates[0] + '年' + dates[1]+'月',
      firstday: e.detail.value+'-01',
    })
    this.getorder();
  },
  bindChange:function(e){
    console.log(e.detail.value);
    var city = this.data.array[e.detail.value];
    this.setData({
      city:city
    })
    var types=this.data.types;
    this.getorder();
  },
  changecaigou:function(){
    if (this.data.types == "采购") {
      return;
    }
    this.setData({
      active:"c",
      types:"采购"
    })
    this.getorder();
  },
  changeyingye: function () {
    if(this.data.types=="营业"){
      return;
    }
    this.setData({
      active: "y",
      types: "营业"
    }) 
    this.getorder();
  },
  getorder:function(){
    var that = this;
    var datas = {};
    datas.City = that.data.city;
    datas.Date = that.data.firstday;
    var orders ='SAMnySum Desc';
    var order ='SAMnySum';
    if (that.data.types =="采购"){
      orders = 'PUMnySum Desc';
      order ='PUMnySum';
    }

    datas.orders=orders;
    datas.where = order+">0";
    //datas.MebID = "124e22579e9a422aa068b422a9193618"
    //datas.MebID = wx.getStorageInfoSync('mebid');
    var datacall = function (res) {
      console.log(res);
      var list = res.data.data;
      var mylist=[];
      var mebid=wx.getStorageInfoSync('mebid');
      //mebid = "124e22579e9a422aa068b422a9193618"
      for(var i=0;i<list.length;i++){
        if (that.data.types == "营业"){
          list[i].Sum = list[i].SAMnySum;
        }else{
          list[i].Sum = list[i].PUMnySum;
        }
        list[i].Sum = post.toDecimal2(list[i].Sum);
        if(list[i].MebID==mebid){
          mylist=list[i];
          mylist.order=i+1;
        }

      }
      that.setData({
        list:list,
        mylist:mylist
      })
    }
    var res2 = post.getdata("/BO/V_SalesRank/Query", datas, datacall);
  }
})